from django.shortcuts import render, redirect,HttpResponse
from django.http import JsonResponse
from web import models
from utils.tencent.cos import delete_bucket

def setting(request,project_id):
    return render(request, 'web/setting.html')


def setting_delete(request, project_id):
    if request.method == "GET":
        return render(request, 'web/setting_delete.html')
    
    else:
        ret = {'status': True, 'msg':''}
        delete_project_name = request.POST.get('delete_project_name')
        if not delete_project_name:
            ret['status'] = False
            ret['msg'] = '输入不能为空'
            return render(request, 'web/setting_delete.html',{'ret': ret})
        
        delete_obj = models.Project.objects.filter(name = delete_project_name,
                                           creator=request.tracer.user).first()
        
        if not delete_obj:
            ret['status'] = False
            ret['msg'] = '项目不存在'
            return render(request, 'web/setting_delete.html',{'ret': ret})
        
        # 删除桶
        delete_bucket(delete_obj.bucket, delete_obj.region)
        
        ret['status']= True
        ret['msg'] = "%s 已删除"%delete_project_name
        # 删除项目
        delete_obj.delete()
        return render(request, 'web/setting_delete.html',{'ret':ret})